﻿using System;
using BCGC.BLL.Helper;
using BCGC.BLL.Forum;
using Jalal.Library.Web;

public partial class show_topic : PublicBasePage
{
    public int PostID
    {
        get
        {
            if (Request.QueryString["id"] != null)
                return Convert.ToInt32(Request.QueryString["id"].ToString());
            else
                return -1;
        }

    }
    public int CategoryID
    {
        get
        {
            if (Request.QueryString["cat"] != null)
                return Convert.ToInt32(Request.QueryString["cat"].ToString());
            else
                return -1;
        }

    }
    private string Subject
    {
        get 
        {
            if (ViewState["_PostSubject"] != null)
                return ViewState["_PostSubject"].ToString();
            else
                return string.Empty;
        }
        set { ViewState["_PostSubject"] = value; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
            if (SessionManager.IsUserLoggedIn)
            {
                pnlSendMessage.Visible = true;

            }
            else
            {
                pnlSendMessage.Visible = false;

            }
        }
    }

    private void LoadData()
    {
        if (PostID != -1)
        {
            Post post=ForumManager.GetPostByID(PostID);
            if (post != null)
            {
                litPostName.Text = post.PostTitle;
                Subject = post.PostTitle;
            }
            repPosts.DataSource=ForumManager.GetPostDetailsByID(PostID);
            repPosts.DataBind();
        }
    }
   
    protected void btnQuickReplySave_Click(object sender, EventArgs e)
    {

        string subject = Subject;
        string content = Server.HtmlEncode(wucPostWriter1.Text);
        if (SessionManager.IsUserLoggedIn)
        {
            try
            {
                if (CategoryID != -1)
                {
                    if (ForumManager.SavePost(string.Empty, CategoryID, SessionManager.LoggedUser.Id, subject, content, DateTime.Now, true, 0, PostID, true))
                    {
                        LoadData();
                    }
                    else { throw new Exception(); }
                }
            }
            catch(Exception ex)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "_MessageBox", "<script>alert('"+ex.Message.ToString()+"')</script>", false);
            }

        }
        else
        {
            Response.Redirect(CustomSiteMap.Login);
        }

       
    }

   
}
